/*
 * Copyright 2010 - Jeremie Brebec
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.jb.coinche.client.business.handler.impl;

import org.jb.coinche.client.business.message.NextTurnMessage;
import org.jb.coinche.client.business.model.Player;
import org.jb.coinche.client.business.model.RoundTurnFinished;
import org.jb.coinche.client.gin.Old;
import org.jb.coinche.client.ui.card.CardList;
import org.jb.coinche.client.ui.card.CardPanel;
import org.jb.coinche.client.ui.card.SetPanel;
import org.jb.coinche.client.ui.card.StatusPanel;
import org.jb.coinche.client.ui.card.StatusPanel.ActionHandler;
import org.jb.statemachine.client.GUIStateHandler;
import org.jb.statemachine.client.MessageSender;
import org.jb.user.client.UserLogin;

import com.google.inject.Inject;

/**
 * Handler pour l'etat "tour fini", on passe au tour suivant
 * @author jeremie
 *
 */
public class GUIRoundTurnFinishedHandler implements GUIStateHandler<RoundTurnFinished> {

	@Inject
	private MessageSender sender;
	
	@Inject
	private SetPanel setPanel;
	
	@Inject @Old
	private SetPanel playedSetPanel;
	
	@Inject
	private CardList cardList;
	
	@Inject
	private StatusPanel statusPanel;
	
	@Inject @UserLogin
	private String user;

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#disable()
	 */
	@Override
	public void disable() {}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#enable()
	 */
	@Override
	public void enable() {}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#update(org.jb.coinche.client.guistatemachine.State)
	 */
	@Override
	public void update(RoundTurnFinished state) {
			
		/*
		 * Mise à jour des cartes sur la table
		 */		
		Player p = state.getGame().getPlayer( user );
		
		final boolean isViewer = ( p == null);

		if (p == null)
			p = state.getGame().getNextPlayer( null );
		
		for( CardPanel cp : setPanel.getCardPanels() ) {	
			cp.setParameters( p.getName(), state.getPlayedCard(p) );
			p = state.getRound().getGame().getNextPlayer( p );
		}
		
		p = state.getGame().getNextPlayer( null );
		for( CardPanel cp : playedSetPanel.getCardPanels() ) {
			cp.setParameters( "", state.getPlayedCard(p) );
			p = state.getRound().getGame().getNextPlayer( p );
		}
		
		/*
		 * Carte restante
		 */
		p = state.getRound().getGame().getPlayer( user );
		if (p != null)
			cardList.setCardList( p.getCards() );
		cardList.setClickable(false);
		
		if (isViewer) {
			statusPanel.setStatus( state.getWinner().getName() + " a gagné ce tour" );
		} else {
			statusPanel.setStatus( state.getWinner().getName() + " a gagné ce tour", "Continuer", new ActionHandler() {
				/*
				 * (non-Javadoc)
				 * @see org.jb.coinche.client.ui.card.StatusPanel.ActionHandler#execute()
				 */
				@Override
				public void execute() {
					sender.send( new NextTurnMessage() );
				}
			} );
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.jb.coinche.client.guistatemachine.GUIStateHandler#getStateClass()
	 */
	@Override
	public Class<RoundTurnFinished> getStateClass() {
		return RoundTurnFinished.class;
	}
}
